
import Vue from 'vue'
import App from './App'
import router from './router'
import 'normalize.css/normalize.css'
import ElementUI from 'element-ui';
Vue.use(ElementUI);
import axios from 'axios'
Vue.prototype.$axios = axios
import 'element-ui/lib/theme-chalk/index.css'

import api from "./interface/index"
 
Vue.prototype.$api = api.commonUrl

Vue.config.productionTip = false

// 设置登录过期时间（一天）
let EXPIRESTIME = 86400000


// 校验登录
router.beforeEach(function(to, from, next) {
  if (to.meta.needLogin) {
    //页面是否登录，本地存储中是否有token(uid)数据，否：跳转登录页面
    let item = localStorage.getItem("token");
    try {
        item = JSON.parse(item);
    } catch (error) {
        // eslint-disable-next-line no-self-assign
        item = item;
    }
    if (item) {
      let date = new Date().getTime();
      // 如果大于就是过期了，如果小于或等于就还没过期
      if (date - item.startTime > EXPIRESTIME) {
          localStorage.removeItem('token');
          next({
            name: "login"
          });
      } else {
        next();
      }
    } else {
      next({
        name: "login"
      });
    }
  } else {
    next();
  }
});



new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
